package cn.com.jenson.service.auth.service.impl;

import cn.com.jenson.dao.user.vo.UserVo;
import cn.com.jenson.service.auth.service.JwtService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.crypto.SecretKey;
import java.util.*;

@Service
public class JwtServiceImpl implements JwtService {
    @Value("${system.jwt.expires}")
    private Integer expires;

    @Value("${system.jwt.secret}")
    private String secret;

    @Value("${system.domain}")
    private String domain;

    @Override
    public String getSecretKey() {
        return secret;
    }

    @Override
    public String generateToken(UserVo userVo, Set<String> authorities) {
        Map<String, Object> tokenInfo = new HashMap<>();
        tokenInfo.put("username", userVo.getName());
        tokenInfo.put("account", userVo.getAccount());
        tokenInfo.put("domain", domain);
        tokenInfo.put("permissions", authorities);
        tokenInfo.put("loginTime", new Date());
        tokenInfo.put("expires", expires);
        SecretKey key = Keys.hmacShaKeyFor(secret.getBytes());
        return Jwts.builder().setClaims(tokenInfo).signWith(key).compact();
    }


}
